package com.hfzy.ihk.web.freeswitchManage.biz;

import com.hfzy.ihk.facade.freeswitchManage.entity.BadCustomerPhone;
import com.hfzy.ihk.web.freeswitchManage.constant.RedisKeys;
import com.hfzy.ihk.web.freeswitchManage.mapper.BadCustomerPhoneMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by czs on 2019/5/28.
 */
@Component
@Slf4j
public class BadCustomerPhoneBiz {
    @Autowired
    private BadCustomerPhoneMapper badCustomerPhoneMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Transactional
    public void saveOrUpdate(BadCustomerPhone badCustomerPhone){
        badCustomerPhoneMapper.saveOrUpdate(badCustomerPhone);

        //保存成功将当前封存的数据保存到redis
        try{
            if(badCustomerPhone.getBadPhone()!=null) {
                redisTemplate.opsForHash().put(RedisKeys.BAD_CUSTOMER_PHONE_MAP, badCustomerPhone.getBadPhone(),1);
                log.info(">>将封存号码更新到redis成功，badPhone:{}",badCustomerPhone.getBadPhone());
            }
        }catch (Exception e){
           log.error(">>将封存号码更新到redis异常，badPhone:{},value:1",badCustomerPhone.getBadPhone());
        }

    }

    public void updateById(BadCustomerPhone badCustomerPhone){
        badCustomerPhoneMapper.updateById(badCustomerPhone);

        //保存成功将当前封存的数据保存到redis
        try{
            if(badCustomerPhone.getBadPhone()!=null) {
                redisTemplate.opsForHash().put(RedisKeys.BAD_CUSTOMER_PHONE_MAP, badCustomerPhone.getBadPhone(),0);
                log.info(">>将封存号码更新到redis成功，badPhone:{}，value:0",badCustomerPhone.getBadPhone());
            }
        }catch (Exception e){
            log.error(">>将封存号码更新到redis异常，badPhone:{}",badCustomerPhone.getBadPhone());
        }
    }
}
